<?php
require_once(dirname(__FILE__)."/../core/safeParam.php");

if($_POST['move'] != ""){

  $update_cat['rid'] = $_POST['rid'];
  $database->update_array("menu_cat", $update_cat, "id =  ".$_POST['cat']."");
  $database->update_array("menu_item", $update_cat, "cat =  ".$_POST['cat']."");	
	
  echo '<p style="color:green">Updated!</p>';
}
$r_rid = $database->select("SELECT * FROM  config ORDER by sitename ");

echo '
<div style="padding:10px;border:1px solid #CCC;margin:10px;">
  <h2>Move a category and its products to a new restaurant</h2>

  <form action="index.php?edit=tools" method="post"> 
    Select a restaurant to move the category to: 
    <select name="rid"><option value="" selected="selected">Select a Restaurant</option>';
    for($i=0; $i<count($r_rid); $i++){
      echo '<option value="'.$r_rid[$i]['id'].'">'.$r_rid[$i]['sitename'].'</option>';
    }
    echo '</select><br><br>
    Select the category you want to move: 
    <select name="cat"><option value="" selected="selected">Select a category</option>';
    for($i=0; $i<count($r_rid); $i++){
      $r_cat = $database->select("SELECT * FROM  menu_cat where rid = ".$r_rid[$i]['id']." ");
      echo ' <optgroup label="'.$r_rid[$i]['sitename'].'">';
      for($j=0; $j<count($r_cat); $j++){
        echo '<option value="'.$r_cat[$j]['id'].'">'.$r_cat[$j]['name'].'</option>';
      }
      echo '</optgroup>';
    }
    echo '</select><br>
    <input type="submit" name="move" value="Move">
  </form>
</div>';
?>
